package com.tripcube.system.controller;

import com.tripcube.common.core.web.controller.BaseController;
import com.tripcube.common.core.web.domain.AjaxResult;
import com.tripcube.common.core.web.page.TableDataInfo;
import com.tripcube.common.log.annotation.Log;
import com.tripcube.common.log.enums.BusinessType;
import com.tripcube.system.domain.SysUserPreferences;
import com.tripcube.system.service.SysUserPreferencesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/user/preferences")
public class SysUserPreferencesController extends BaseController {

    @Autowired
    private SysUserPreferencesService userPreferencesService;

    /**
     * 获取用户偏好设置列表
     * @param preferences 查询参数
     * @return 用户偏好设置列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysUserPreferences preferences){
        startPage();
        List<SysUserPreferences> sysUserPreferences1 = userPreferencesService.selectUserPreferencesList(preferences);
        return getDataTable(sysUserPreferences1);
    }

    /**
     * 根据用户ID获取用户偏好设置
     * @param userId 用户ID
     * @return 用户偏好设置对象
     */
    @GetMapping("/getByUserId/{userId}")
    public AjaxResult getByUserId(@PathVariable Long userId) {
        SysUserPreferences preferences = userPreferencesService.getByUserId(userId);
        return AjaxResult.success(preferences);
    }

    /**
     * 为指定用户初始化默认偏好设置
     * @param userId 用户ID
     * @return 操作结果
     */
    @PostMapping("/initDefault/{userId}")
    @Log(title = "用户偏好", businessType = BusinessType.INSERT)
    public AjaxResult initDefault(@PathVariable Long userId) {
        userPreferencesService.initDefault(userId);
        return AjaxResult.success("初始化成功");
    }

    /**
     * 根据用户ID更新偏好设置
     * @param dto 用户偏好设置数据传输对象
     * @return 操作结果
     */
    @PutMapping("/updateByUserId")
    @Log(title = "用户偏好", businessType = BusinessType.UPDATE)
    public AjaxResult updateByUserId(@RequestBody SysUserPreferences dto) {
        userPreferencesService.updateByUserId(dto);
        return AjaxResult.success("更新成功");
    }

    /**
     * 判断用户兴趣是否匹配指定城市标签
     * @param userId 用户ID
     * @param cityTag 城市标签
     * @return 是否匹配
     */
    @GetMapping("/isInterestMatch")
    public AjaxResult isInterestMatch(@RequestParam Long userId, @RequestParam String cityTag) {
        boolean result = userPreferencesService.isInterestMatch(userId, cityTag);
        return AjaxResult.success(result);
    }
}
